package com.pilipili.zeus.eduService.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pilipili.commonutils.Result;
import com.pilipili.zeus.eduService.entity.EduCourse;
import com.pilipili.zeus.eduService.entity.vo.CourseInfo;
import com.pilipili.zeus.eduService.entity.vo.Publish;
import com.pilipili.zeus.eduService.entity.vo.QueryCourse;
import com.pilipili.zeus.eduService.entity.vo.SortCourse;
import com.pilipili.zeus.eduService.service.EduCourseService;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * 番剧 前端控制器
 *
 * @author Unlimited_Blade_Works
 * @since 2021-05-26
 */
@RestController
@RequestMapping("/eduService/course")
public class EduCourseController {
    @Autowired
    private EduCourseService courseService;

    @ApiOperation(value = "读取courseExcel中的表格,写入数据库")
    @PostMapping("addCourses")
    public Result addCourses(MultipartFile file){
        courseService.saveCourseDataByExcel(file,courseService);
        return Result.ok();
    }

    @ApiModelProperty(value = "添加番剧信息,返回番剧ID")
    @PostMapping("addCourseInfo")
    public Result addCourseInfo(@RequestBody CourseInfo courseInfo){
        String courseId = courseService.saveCourseInfo(courseInfo);
        return Result.ok().data("courseId",courseId);
    }

    @ApiOperation(value = "查询所有番剧")
    @GetMapping("findAll")
    public Result findAll(){
        List<EduCourse> list = courseService.list(null);
        return Result.ok().data("items",list);
    }

    @ApiOperation(value = "根据id查询番剧信息")
    @GetMapping("getCourseInfo/{courseId}")
    public Result getCourseInfoById(@PathVariable String courseId){
        CourseInfo courseInfo=courseService.getCourseInfo(courseId);
        return Result.ok().data("courseInfo",courseInfo);
    }

    @ApiOperation(value = "根据id查询番剧信息")
    @GetMapping("getPublishCourseInfo/{courseId}")
    public Result getPublishCourseInfoById(@PathVariable String courseId){
        Publish courseInfo=courseService.getPublishCourseInfo(courseId);
        return Result.ok().data("publishCourseInfo",courseInfo);
    }



    @ApiOperation(value = "发布番剧")
    @PostMapping("publishCourse/{courseId}")
    public Result publishCourse(@PathVariable String courseId){
        EduCourse eduCourse = new EduCourse();
        eduCourse.setId(courseId);
        eduCourse.setStatus(1);
        courseService.updateById(eduCourse);
        return Result.ok();
    }

    @ApiOperation(value = "修改番剧信息")
    @PostMapping("updateCourseInfo")
    public Result updateCourseInfo(@RequestBody CourseInfo courseInfo){
        courseService.updateCourseInfo(courseInfo);
        return Result.ok();
    }

    @ApiOperation(value = "条件+分页查询")
    @PostMapping("pageCondition/{current}/{limit}")
    public Result pageCondition(@PathVariable long current,
                                @PathVariable long limit,
                                @RequestBody(required = false) QueryCourse query){
        Page<EduCourse> page=new Page<>(current,limit);
        courseService.pageQuery(page, query);
        List<EduCourse> list = page.getRecords();
        long total = page.getTotal();
        return  Result.ok().data("total", total).data("list",list);
    }

    @ApiOperation(value = "按番剧id删除全部子类")
    @DeleteMapping("{courseId}")
    public Result deleteCourse(@PathVariable String courseId){
        courseService.removeCourse(courseId);
        return  Result.ok();
    }

}

